package org.gjy.m8.thread;

import com.google.common.base.Stopwatch;
import org.joda.time.DateTime;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-08-19 10:13:22
 */
public class SimTest {

    private static int sum = 0;

    @Test
    public void testSimpleMockLock() throws InterruptedException {
        int turns = 1000;
        int threads = 10;
        ExecutorService pool = Executors.newFixedThreadPool(threads);
        Lock lock = new SimpleMockLock();
        CountDownLatch cdl = new CountDownLatch(threads);
        Stopwatch started = Stopwatch.createStarted();

        for (int i = 0; i < threads; i++) {
            pool.submit(() -> {
                for (int j = 0; j < turns; j++) {
                    lock.lock();
                    sum++;
                    lock.unlock();
                }
                cdl.countDown();
            });
        }

        cdl.await();

        long elapsed = started.elapsed(TimeUnit.MILLISECONDS);
        System.out.println("elapsed = " + elapsed);
        System.out.println("sum = " + sum);
    }

    @Test
    public void test1() {
        String a = "我爱中国";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 500; i++) {
            sb.append(a);
        }
        System.out.println(sb.length());
        System.out.println(sb);
    }

    @Test
    public void test2() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date expireDate = new DateTime(new Date())
                .plusDays(Integer.parseInt("1"))
                .millisOfDay().withMaximumValue().toDate();
        System.out.println(format.format(expireDate));
    }

    @Test
    public void test3() {
        try {
            System.out.println("A");
            int a = 1 / 0;
        } catch (Exception e) {
            System.out.println("B");
            return;
        }
        System.out.println("C");
    }
}
